package com.frinder.hadoop.job.electricity;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * @author frinder
 * @date 2018/6/25
 * @Description: ${todo}
 */
public class ElectricityMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String lastToken = null;
        StringTokenizer s = new StringTokenizer(line, " ");
        String year = s.nextToken();
        while (s.hasMoreTokens()) {
            lastToken = s.nextToken();
        }
        if (null != lastToken) {
            int avgPrice = Integer.parseInt(lastToken);
            context.write(new Text(year), new IntWritable(avgPrice));
        }
    }

}
